package com.mapbased.wfw;

import java.util.*;
import java.io.File;
import java.io.*;
import java.net.MalformedURLException;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class DefaultConfig
    implements Config
{
    private Properties properties=new Properties();
    //java.net.URL location;
    public Object[] keySet()
    {
        return this.properties.keySet().toArray();
    }

    /**
     * getValue
     *
     * @param key String
     * @return String
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public String getValue(String key)
    {
        return this.properties.getProperty(key,"");

    }

    public String getValue(String key,String defaultValue)
    {
        return this.properties.getProperty(key,defaultValue);
    }

    public void list(PrintStream ps)
    {
        this.properties.list(ps);
    }

    private void doLoad(PageLoader loader)
        throws MalformedURLException
    {
        this.properties.clear();
        String WFW_CONFIG="/WEB-INF/wfw-config.properties";
        Config c=loader.getGlobal().getWfwConfig();

        java.net.URL u=loader.getResourceFromFS(PageLoader.
                                                GLOBAL_PAGE_PATH,WFW_CONFIG);
        if(u != null)
        {
            c.load(u);
        }
        u=loader.getResourceFromFS(loader.getDefaulPagePath(),WFW_CONFIG);
        if(u != null)
        {
            c.load(u);
        }
        else
        {
            u=loader.getResourceFromWar(WFW_CONFIG);
            if(u != null)
            {
                c.load(u);
            }
        }

    }

    public void postLoad(PageLoader loader)
    {
        try
        {
            doLoad(loader);

            String s=this.getValue("develope-mode","false").trim();
            loader.setDevelopeMode("true".equals(s));

        }
        catch(MalformedURLException ex)
        {
            ex.printStackTrace();
        }

        Object[] keys=this.keySet();
        for(int i=0;i < keys.length;i++)
        {
            String ki=keys[i].toString();
            String v=this.getValue(ki);
            if(v.indexOf('@') >= 0)
            {
                List segs=new ArrayList(4);
                PageLoader.StringBufferBag bag=new PageLoader.
                    StringBufferBag();

                loader.parseBinding(
                    v,segs,bag);
                if(bag.buffer.length() > 0)
                {
                    segs.add(bag.getString());
                }
                if(segs.size() != 1)
                {
                    throw new java.lang.RuntimeException(
                        "There should only @config in config file,key:"+ki);
                }
                this.putValue(ki,String.valueOf(segs.get(0)));

            }
            // ((DefaultConfig)c).list(System.out);

        }

    }

    /**
     * load
     *
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public void load(java.net.URL i)
    {

        try
        {

            this.properties.load(i.openStream());
            //     this.properties.list(System.out);
        }
        catch(Exception ex)
        {

            System.err.println(ex.getMessage());
            //   ex.printStackTrace();
        }
    }

    /**
     * putValue
     *
     * @param key String
     * @param value String
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public void putValue(String key,String value)
    {
        this.properties.setProperty(key,value);
    }

    /**
     * save
     *
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public void save()
    {

        throw new java.lang.RuntimeException("Not implemented");

    }

    /**
     * getInt
     *
     * @param key String
     * @param defaultValue int
     * @return int
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public int getInt(String key,int defaultValue)
    {
         return Integer.parseInt( this.getValue(key,defaultValue+""));
    }

    /**
     *
     * @param key String
     * @param defaultValue String ,end with
     *   d=day;s=second;ms;m=;h=hour;M=mounth;w=week;y=year
     * @return long
     * @todo Implement this com.mapbased.wfw.Config method
     */
    public long getInterval(String key,String defaultValue)
    {
        return com.mapbased.util.StringNumber.getInterval(this.getValue(key,defaultValue));
    }
}
